package com.ljcalc.business.components;


import java.util.LinkedList;

import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.XMLString;


public class NumberPercentageItemListHandler extends SaxHandler {

	private LinkedList<NumberPercentage> list = new LinkedList<NumberPercentage>();
	private NumberPercentage np = new NumberPercentage();
	private boolean foundNumber = false;
	private boolean startRecording = false;
	
	public NumberPercentageItemListHandler(){
		super();
	}
	
	public void startElement(QName element, XMLAttributes attrs,
			Augmentations augs) {
		String clazz = attrs.getValue("class");
		if(!startRecording && clazz!=null && clazz.equalsIgnoreCase("sbb")){
			startRecording = true;
		}
		
	}

	public void endElement(QName element, Augmentations augs) {
		

    }
	
    /** Characters. */
    public void characters(XMLString text, Augmentations augs){
    	if(startRecording){
    		try{
    			long l = Long.parseLong(text.toString()) ;
        		foundNumber = true;
        		
        		if(!np.isNumModified()){
        			np.setNum(l);
        		}else if(!np.isPerModified()){
        			np.setPer(l);
        		}
        		if(np.hasValues()){
        			list.add(new NumberPercentage(np.getNum(),np.getPercentage()));
        			np.clearFlags();
        		}
        		
    		}catch(NumberFormatException ex){
    			if(foundNumber){
    				startRecording = false;
    			}
    		}
    	}

    } // characters(XMLString,Augmentations)


	public LinkedList<NumberPercentage> getList() {
		// TODO Auto-generated method stub
		return list;
	}
	
	
	

}
